<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Goods;
use app\common\model\GoodsList;
use app\common\model\Order;
use app\common\model\User;
use app\common\model\UserCoupon;
use app\common\model\Shang;
use app\common\model\OrderList;
use app\common\model\UserVip;
use app\common\model\CouponReceive as CouponReceiveModel;
use app\common\model\Climb as ClimbModel;
use app\common\model\ClimbUser;
use app\common\model\ClimbRank;

use \think\Request;

class Climb extends Base
{
    
    /**
     * 所有爬塔盒子
     */ 
    public function climb_list(Request $request)
    {
        // $user = $this->getUser();
        $w = [];
        $w[] = ['status', '=', '1']; //已起用
        $w[] = ['type', '=', '11'];
        $field = "id,title, floors_num,imgurl";
        $climb_list = Goods::getList($w, $field,'id desc');
        foreach ($climb_list['list'] as $k => &$v) {
           $v['imgurl'] = imageUrl($v['imgurl']);
        }
        return $this->renderSuccess("请求成功", compact('climb_list'));

    }
    /**
     * 爬塔页面
     */
    public function index(Request $request)
    {
        $goods_id = $request->param('goods_id/d');
        if (empty($goods_id)) {
            return $this->renderError("缺少参数：goods_id");
        }
        $user = $this->getUser();
        $w = [];
        $w[] = ['status', '=', '1']; //已起用
        $w[] = ['type', '=', '11'];
        $w[] = ['id', '=', $goods_id];
        $field = "id,title, floors_num";
        $climb = Goods::getInfo($w, $field);
        if (empty($climb)) {
            return $this->renderError("传参有误");
        }
        //用户当前关卡
        $list = [];
        $w = [];
        $w[] = ['user_id', '=', $user['id']];
        $w[] = ['goods_id', '=', $goods_id];
        $num = ClimbUser::where($w)->order('id desc')->value('num');
        if (empty($num)) {
            $num = 1;
        } 
        for ($i = 1; $i <= $climb['floors_num']; $i++) {
            $n = $i;
            $list[$i]['num'] = $n;
            if ($n < $num) {
                $list[$i]['is_unlock'] = 1; //  已解锁
            } elseif ($n == $num) {
                $list[$i]['is_unlock'] = 2; //当前关卡
            } else {
                $list[$i]['is_unlock'] = 3; //  未解锁
            }

        }
        
        
        array_multisort(array_column($list, 'num'), SORT_DESC, $list);
//        $list = array_reverse($list);
        //查询现有排行榜的前三名
        $w = [];
        $w[] = ['goods_id', '=', $climb['id']];
        $field = "id,user_id, count(number) as number";
        $order = "number desc, id desc";
        $rank = ClimbRank::where($w)->field($field)->group('user_id')->order($order)->paginate(['list_rows' => 3, 'query' => request()->param()]);
        $ranks = $rank->toArray()['data'];
        foreach ($ranks as $k => &$v) {
            $user_info = User::getInfo(['id' => $v['user_id']], 'id,nickname');
            $v['nickname'] = $user_info['nickname'];
        }
        $bgk_music = imageUrl('/LiSA%20-%20crossing%20field.mp3');
        return $this->renderSuccess("请求成功", compact('climb', 'list', 'num', 'ranks','bgk_music'));
    }

    /**
     * 爬塔详情
     */
    public function detail(Request $request)
    {
        $user = $this->getUser();
        $goods_id = $request->param('goods_id/d');
        $num = $request->param('num/d', '0');

        $w = [];
        $w[] = ['id', '=', $goods_id];
        $w[] = ['status', '=', '1'];
        $w[] = ['type', '=', '11'];
        $goods = goods::getInfo($w, 'id,title,type,price,imgurl');
        if (empty($goods)) {
            return $this->renderError("爬塔盒子不存在");
        }
        if (empty($num)) {
            //查询当前处于第几关
            $ww = [];
            $ww[] = ['user_id', '=', $user['id']];
            $ww[] = ['goods_id', '=', $goods_id];
            $num = ClimbUser::where($ww)->order("id desc")->value("num");
            if (empty($num)) {
                $num = 1;
            }
        }
        $w = [];
        $w[] = ['goods_id', '=', $goods_id];
        $w[] = ['num', '=', $num];
        $price = ClimbModel::where($w)->value('price');
        $goods['price'] = $price;
        $goods['imgurl'] = imageUrl($goods['imgurl']);

        //无限商品
        $ww = [];
        $ww[] = ['g.goods_id', '=', $goods_id];
        $ww[] = ['g.status', '=', '1'];
        $ww[] = ['g.num', '=', $num];
        $ww[] = ['g.prize_type', '<', 5];
        $field = "g.shang_id,sum(g.gailv) as gailv,s.title as shang_title,s.imgurl";
        $order = "s.id desc,g.id desc";
        $shang = GoodsList::alias("g")
            ->join('shang s', 'g.shang_id = s.id')
            ->where($ww)
            ->group("shang_id")
            ->order($order)
            ->field($field)
            ->select()->toArray();
        $gailv = array_column($shang, 'gailv');
        $gailv_tal = array_sum($gailv);
        $goods_list = [];
        foreach ($shang as $k => &$v) {
            $v['imgurl'] = imageUrl($v['imgurl']);
            if ($gailv_tal > 0) {
                $v['gailv'] = jisuan(jisuan($v['gailv'], $gailv_tal, '/', 6), 100, '*', 3) . '%';
            } else {
                $v['gailv'] = '0%';
            }
            $w = [];
            $w[] = ['goods_id', '=', $goods_id];
            $w[] = ['shang_id', '=', $v['shang_id']];
            $w[] = ['status', '=', '1'];
            $w[] = ['num', '=', $num];
            $w[] = ['prize_type', '<', 5];
            $infinite_list = GoodsList::getAllList($w, 'id,title,imgurl,price,goods_type,prize_type', 'price desc,sort asc,id desc');
            foreach ($infinite_list as $key => &$val) {
                $val['imgurl'] = imageUrl($val['imgurl']);
                $val['shang_id'] = $v['shang_id'];
                $val['shang_title'] = $v['shang_title'];
                $goods_list[] = $val;
            }
            $v['goods_list'] = $infinite_list;
        }
        return $this->renderSuccess("请求成功", compact('goods', 'shang', 'goods_list'));
    }

    /**
     * 商品详情
     */
    public function climb_list_detail(Request $request)
    {
        $goodslist_id = $request->param("goodslist_id/d");
        if (empty($goodslist_id)) {
            return $this->renderError("缺少必要的参数");
        }
        $w = [];
        $w[] = ['id', '=', $goodslist_id];
        $goodslist = GoodsList::getInfo($w, 'id,infinite_id,shang_id,title,price,imgurl,content');
        $goodslist['imgurl'] = imageUrl($goodslist['imgurl']);
        return $this->renderSuccess("请求成功", $goodslist);
    }
    
    //首通奖励列表
    public function first_prize_list(Request $request)
    {
        $goods_id = $request->param('goods_id/d');
        $w = [];
        $w[] = ['goods_id', '=', $goods_id];
        $w[] = ['status', '=', '1'];
        $w[] = ['prize_type', '=', 5];
        $infinite_list = GoodsList::getAllList($w, 'id,title,imgurl,price,goods_type,prize_type, num', 'num asc');
        foreach ($infinite_list as $key => &$val) {
            $val['imgurl'] = imageUrl($val['imgurl']);
        }
        return $this->renderSuccess("请求成功", $infinite_list);
    }
    
    // 通关奖励
    public function end_prize_list(Request $request)
    {
        $goods_id = $request->param('goods_id/d');
        $w = [];
        $w[] = ['goods_id', '=', $goods_id];
        $w[] = ['status', '=', '1'];
        $w[] = ['prize_type', '=', 4];
        $infinite_list = GoodsList::getAllList($w, 'id,title,imgurl,price,goods_type,prize_type, gailv', 'sort asc');
        $gailv_tal = GoodsList::where($w)->sum('gailv');
        foreach ($infinite_list as $key => &$val) {
            $val['imgurl'] = imageUrl($val['imgurl']);
            if ($gailv_tal > 0) {
                $val['gailv'] = jisuan(jisuan($val['gailv'], $gailv_tal, '/', 6), 100, '*', 3) . '%';
            } else {
                $val['gailv'] = '0%';
            }
        }
        return $this->renderSuccess("请求成功", $infinite_list);
    }
    /**
     * 抽赏记录
     */
    public function climb_list_info(Request $request)
    {
        $goods_id = $request->param("goods_id/d");
        $shang_id = $request->param("shang_id/d");
        $num = $request->param("num/d");
        $w = [];
        if (!empty($shang_id)) {
            $w[] = ['shang_id', '=', $shang_id];
        }
        if (!empty($num)) {
            $w[] = ['num', '=', $num];
        }
        $w[] = ['goods_id', '=', $goods_id];
        $w[] = ['order_type', '=', '11'];
        $w[] = ['prize_type', '<', 4];
        $field = 'goodslist_title, goodslist_id, goodslist_imgurl, user_id,  goodslist_price,  shang_id, goodslist_type, goodslist_sale_time, prize_type, order_type, luck_no, addtime';
        $order = 'id desc';
        $record = OrderList::where($w)->field($field)->order($order)->paginate(['list_rows' => $this->page, 'query' => request()->param()]);

        $data['list'] = $record->toArray()['data'];
        foreach ($data['list'] as $k => &$v) {
            $v['imgurl'] = imageUrl($v['goodslist_imgurl']);
            $userinfo = User::getInfo(['id' => $v['user_id']], 'nickname,headimg');
            $v['nickname'] = $userinfo['nickname'];
            $v['headimg'] = imageUrl($userinfo['headimg']);
            $shang = Shang::getInfo(['id' => $v['shang_id']], 'id,title');
            $v['shang_title'] = $shang['title'];
            $v['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
        }
        $page = $record->render();
        $data['count'] = $record->total();
        $data['last_page'] = $record->toArray()['last_page'];
        $data['page'] = $page;
        return $this->renderSuccess("请求成功", compact('data'));
    }

    /**
     * 抽赏记录2
     * @param Request $request
     * @return mixed
     */
    public function climb_list_info_cp(Request $request)
    {
        $goods_id = $request->param("goods_id/d");
        $num = $request->param("num/d");
        $w = [];
        if (!empty($num)) {
            $w[] = ['num', '=', $num];
        }
        $w[] = ['goods_id', '=', $goods_id];
        $w[] = ['order_type', '=', '11'];
        $shang_ids = OrderList::where($w)->column('shang_id');
        $shang_ids = array_unique($shang_ids);
        $field = 'goodslist_title, goodslist_id, goodslist_imgurl, user_id,  goodslist_price,  shang_id, goodslist_type, goodslist_sale_time, prize_type, order_type, luck_no, addtime';
        $order = 'id desc';
        $result = [];
        foreach ($shang_ids as $shang_id) {
            $w = [];
            if (!empty($num)) {
                $w[] = ['num', '=', $num];
            }
            $w[] = ['goods_id', '=', $goods_id];
            $w[] = ['order_type', '=', '11'];
            $w[] = ['shang_id', '=', $shang_id];
            $record = OrderList::where($w)->field($field)->order($order)->paginate(['list_rows' => 200, 'query' => request()->param()]);
            $data['list'] = $record->toArray()['data'];
            foreach ($data['list'] as $k => &$v) {
                $v['imgurl'] = imageUrl($v['goodslist_imgurl']);
                $userinfo = User::getInfo(['id' => $v['user_id']], 'nickname,headimg');
                $v['nickname'] = $userinfo['nickname'];
                $v['headimg'] = imageUrl($userinfo['headimg']);
                $shang = Shang::getInfo(['id' => $v['shang_id']], 'id,title');
                $v['shang_title'] = $shang['title'];
                $v['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            }
            $result[] = $data['list'];
        }
        $page = $record->render();
        $data['count'] = $record->total();
        $data['last_page'] = $record->toArray()['last_page'];
        $data['page'] = $page;
        return $this->renderSuccess("请求成功", compact('result'));
    }
    /**
     * 提交订单
     */
    public function order_money_info(Request $request)
    {
        $user = $this->getUser();
        $goods_id = $request->param('goods_id/d');
        $prize_num = $request->param('prize_num/d', '1');
        $num = $request->param('num/d');
        $use_money_is = request()->param('use_money_is/d', 0);     // 余额抵扣  #0不抵扣 1抵扣
        $use_integral_is = request()->param('use_integral_is/d', 0); #0不抵扣 1抵扣
        $coupon_id = request()->param('coupon_id/d', 0);  //优惠券
        if (empty($goods_id)) {
            return $this->renderError("缺少盒子");
        }
        $w = [];
        $w[] = ['id', '=', $goods_id];
        $w[] = ['type', '=', '11'];
        $goods = Goods::getInfo($w, 'id,title,imgurl_detail,type,price,status,is_shou_zhe,max_num,imgurl,end_time');
        
        $where = [];
        $where[] = ['user_id', '=', $user['id']];
        $where[] = ['goods_id', '=', $goods_id];
        $order_count = OrderList::where($where)->field('id')->count('id');
        if ($goods['max_num'] > 0 and $order_count >= $goods['max_num']) {
            return $this->renderError("已达上限");
        }
        
        if (empty($goods)) {
            return $this->renderError("参数错误哦！");
        }
        if ($goods['end_time'] <= time()) {
            return $this->renderError("赛季结束了");
        }
        if ($goods['status'] != '1') {
            return $this->renderError("盒子已关闭");
        }
        //查询当前处于第几关
        $ww = [];
        $ww[] = ['user_id', '=', $user['id']];
        $ww[] = ['goods_id', '=', $goods_id];
        $current_num = ClimbUser::where($ww)->order("id desc")->value("num");
        if (empty($current_num)) {
            $current_num = 1;
        }
        if ($current_num != $num) {
            $current_num = $num;
        }
        $w = [];
        $w[] = ['goods_id', '=', $goods_id];
        $w[] = ['num', '=', $current_num];
        $goods['price'] = ClimbModel::where($w)->value('price');
        $goods['imgurl'] = imageUrl($goods['imgurl']);
        $box_price = $goods['price'];
        //是否首抽五折
        $shou_zhe_price = 0;
        if ($goods['type'] != 5) {
            $is_chou = Order::field('id')->where([['user_id', '=', $user['id']], ['status', '=', 1]])->find();
            $is_chou2 = Order::field('id')->where([['is_shou_zhe', '=', 1], ['status', '=', 1], ['user_id', '=', $user['id']]])->find();
            if (!$is_chou && !$is_chou2 && $goods['is_shou_zhe'] == 1) {
                $shou_zhe_price = bcmul("$box_price", "0.5", 2);
            }
        }
        $goods['shou_zhe_price'] = $shou_zhe_price;
        $price = bcmul("$box_price", "$prize_num", 2);
        $price = bcsub("$price", "$shou_zhe_price", 2);
        #订单金额 微信支付金额
        $order_total = $order_zhe_total = $price;

        #首抽半价
        if ($shou_zhe_price <= 0) {
            #使用优惠券
            $coupon_price = 0;
            if (!empty($coupon_id)) {
                $coupon = CouponReceiveModel::where(['id' => $coupon_id, 'status' => 0, 'user_id' => $user['id']])->where('man_price', '<=', $price)->find();
                if ($coupon) {
                    $coupon_price = $coupon['price'];
                }

            } else {
                $coupon_id = 0;
                $coupon = CouponReceiveModel::where(['status' => 0, 'user_id' => $user['id']])->where('man_price', '<=', $price)->order('price desc')->find();
                if ($coupon) {
                    $coupon_price = $coupon['price'];
                    $coupon_id = $coupon['id'];
                }
            }
            $price = bcsub("$price", "$coupon_price", 2);

            if ($price <= 0) {
                $price = 0;
            }
            $order_zhe_total = $price;
            $zhe = 0;
            $vip_info = UserVip::where(['id' => $user['vip']])->find();
            if ($vip_info && $vip_info['discount'] > 0) {  //享受折扣
                $zhe = $vip_info['discount'];
                $zhe_bl = bcdiv("$zhe", "10", 2);
                $order_zhe_total = $price = bcmul("$price", "$zhe_bl", 2);
            }

            #余额抵扣
            $use_money = 0;
            if ($use_money_is == 1) {
                if ($user['money'] >= $price) {
                    $use_money = $price;
                    $price = 0;
                } else {
                    $use_money = $user['money'];#
                    $price = bcsub("$price", "{$user['money']}", 2);
                }
            }
        } else {
            $coupon_id = 0;
            $coupon_price = 0;
//            $use_integral = 0;
            $use_money = 0;
            $zhe = 0;
        }
        #抽奖数量
        $goods['prize_num'] = $prize_num;
        $ww = [];
        $ww[] = ['user_id', '=', $user['id']];
        $ww[] = ['goods_id', '=', $goods_id];
        $current_num = ClimbUser::where($ww)->order("id desc")->value("num");
        $data = [
            'goods' => $goods,
            'order_total' => round(floatval($order_total), 2),
            'order_zhe_total' => round($order_zhe_total, 2),
            'zhe' => round($zhe, 2),
            'price' => round($price, 2),
//            'integral'      => round($user['integral'],2),
//            'use_integral'  => round($use_integral,2),
//            'use_integral_money' => round(round($use_integral,2) /100,2),
            'money' => round($user['money'], 2),
            'use_money' => round($use_money, 2),
            'coupon_id' => $coupon_id,
            'coupon_price' => round($coupon_price, 2),
            'current_num' => $current_num,
        ];
        return $this->renderSuccess("请求成功", $data);
    }

    /**
     * 盲盒提交订单
     */
    public function money_add()
    {
        $user = $this->getUser();
        if (empty($user['mobile'])) {
            return $this->renderError('请先绑定手机号', [], -9);
        }
        $ad_id = request()->header('adid');
        $goods_id = request()->param('goods_id/d', 0);               #盒子ID
        $prize_num = request()->param('prize_num/d', 0);             #抽几发
        $use_money_is = request()->param('use_money_is/d', 0);       #0不抵扣 1抵扣
        $use_integral_is = request()->param('use_integral_is/d', 0); #0不抵扣 1抵扣
        $num = request()->param('num/d');
        $coupon_id = request()->param('coupon_id');  //优惠券

        #盒子信息
        $goods = Goods::field('id,title,imgurl_detail,type,price,status,is_shou_zhe,max_num')->where(['id' => $goods_id])
            ->find();

        if (!$goods) {
            return $this->renderError("盒子不存在");
        }
        $where = [];
        $where[] = ['user_id', '=', $user['id']];
        $where[] = ['goods_id', '=', $goods_id];
        $order_count = OrderList::where($where)->field('id')->count('id');
        if ($goods['max_num'] > 0 and $order_count >= $goods['max_num']) {
            return $this->renderError("已达上限");
        }
        if ($goods['status'] != 1) {
            return $this->renderError("盒子已下架");
        }

        $where = [];
        $where[] = ['goods_id', '=', $goods_id];
        $where[] = ['num', '=', $num];
        $where[] = ['gailv', '>', 0];
        $where[] = ['shang_id', 'between', [35,38]];

        $is_goodslist = GoodsList::field('id')
            ->where($where)
            ->find();
        if (!$is_goodslist) {
            return $this->renderError('暂无奖品信息');
        }

   
        //查询当前处于第几关
        $ww = [];
        $ww[] = ['user_id','=',$user['id']];
        $ww[] = ['goods_id','=',$goods_id];
        $real_num = ClimbUser::where($ww)->order("id desc")->value("num");
        if(empty($real_num)){
            $real_num = 1;
        }
        if($real_num != $num){
            $num = $real_num;
        }
        $w = [];
        $w[] = ['goods_id','=',$goods_id];
        $w[] = ['num','=',$num];
        $goods['price'] = ClimbModel::where($w)->value('price');
        #盒子单价
        $box_price = $goods['price'];
        //是否首抽五折
        $shou_zhe_price = 0;
        $is_shou_zhe = 0;
        if ($goods['type'] != 5) {
            $is_chou = Order::field('id')->where([['user_id', '=', $user['id']], ['status', '=', 1]])->find();
            $is_chou2 = Order::field('id')->where([['is_shou_zhe', '=', 1], ['status', '=', 1], ['user_id', '=', $user['id']]])->find();
            if (!$is_chou && !$is_chou2 && $goods['is_shou_zhe'] == 1) {
                $shou_zhe_price = bcmul("$box_price", "0.5", 2);
                $is_shou_zhe = 1;
            }
        }

        $goods['shou_zhe_price'] = $shou_zhe_price;
        $price = bcmul("$box_price", "$prize_num", 2);
        $price = bcsub("$price", "$shou_zhe_price", 2);
        #订单金额 微信支付金额
        $order_total = $order_zhe_total = $price;

        #使用折扣
        if ($is_shou_zhe == 1) {
            $coupon_id = 0;
            $coupon_price = 0;
            $use_integral = 0;
            $use_money = 0;
            #折扣
            $zhe = 0;
        } else {
            #使用优惠券
            $coupon_price = 0;
            if (!empty($coupon_id)) {
                $coupon = CouponReceiveModel::where(['id' => $coupon_id, 'status' => 0, 'user_id' => $user['id']])->where('man_price', '<=', $price)->find();
                if ($coupon) {
                    $coupon_price = $coupon['price'];
                }
            }
            $price = bcsub("$price", "$coupon_price", 2);

            if ($price <= 0) {
                $price = 0;
            }
            $order_zhe_total = $price;

            #折扣
            $zhe = 0;
            $vip_info = UserVip::where(['id' => $user['vip']])->find();
            if ($vip_info && $vip_info['discount'] > 0) {
                $zhe = $vip_info['discount'];
                $zhe_bl = bcdiv("$zhe", "10", 2);
                $order_zhe_total = $price = bcmul("$price", "$zhe_bl", 2);
            }
            #潮币抵扣
            $use_integral = 0;
            #余额抵扣
            $use_money = 0;
            if ($use_money_is == 1) {
                if ($user['money'] >= $price) {
                    $use_money = $price;
                    $price = 0;
                } else {
                    $use_money = $user['money'];#
                    $price = bcsub("$price", "{$user['money']}", 2);
                }
            }
        }


        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis_key = "pata_money_buy" . '_' . $user['id'];
        $redis_key_info = $redis->get($redis_key);
        if ($redis_key_info) {
            return $this->renderError("当前操作太快了,请等待");
        } else {
            $redis->set($redis_key, 1, 6);
        }
        CouponReceiveModel::startTrans();
        $res = [];
        $order_num = create_order_no('MH_', 'order', 'order_num');
        #创建订单
        $res[] = $order_id = Order::insertGetId([
            'user_id' => $user['id'],
            'order_num' => $order_num,
            'order_total' => $order_total,#订单金额
            'order_zhe_total' => $order_zhe_total,#订单折扣金额
            'price' => $price,#微信支付
            'use_money' => $use_money,#余额抵扣
            'use_integral' => $use_integral,#潮币抵扣
            'use_score' => 0,#积分抵扣
            'zhe' => $zhe,#会员折扣
            'goods_id' => $goods_id,
            'num' => $num,
            'goods_price' => $goods['price'],
            'goods_title' => $goods['title'],
            'goods_imgurl' => $goods['imgurl_detail'],
            'prize_num' => $prize_num,
            'status' => 0,
            'pay_type' => 1,#1微信 2支付宝
            'order_type' => $goods['type'],
            'addtime' => time(),
            'coupon_id' => $coupon_id ? $coupon_id : 0,
            'use_coupon' => $coupon_price, #优惠券抵扣
            'is_shou_zhe' => $is_shou_zhe,
            'ad_id' => $ad_id,
            'click_id' => $user['click_id']
        ]);
        #微信支付金额大于0
        if ($price > 0) {
            $body = '购买爬塔盒子' . $goods['title'];
            $attach = 'order_climb';
            $payRes = (new Pay())->wxCreateOrder($order_num, $price, $user['openid'], $body, $attach);
            if ($payRes['status'] == 1) {
                #结果集
                $new_data = [
                    'status' => 1,
                    'order_num' => $order_num,
                    'order_id' => $order_id,
                    'res' => $payRes['data'],
                ];
            } else {
                #删除redis
                $redis->del($redis_key);
                OrderList::rollback();
                return $this->renderError("下单失败");
            }
        } else {
            #开盒子
            $res[] = $ret = (new Notify($this->app))->pata_drawprize_notice($user['id'], $order_id, $goods_id, $num);
            #结果集
            $new_data = [
                'status' => 0,
                'order_num' => $order_num,
                'order_id' => $order_id,
                'num'  => $num,
            ];
        }

        if (resCheck($res)) {
            $redis->del($redis_key);
            CouponReceiveModel::commit();
            return $this->renderSuccess("下单成功啦", $new_data);
        } else {
            CouponReceiveModel::rollback();
            #删除redis
            $redis->del($redis_key);
            return $this->renderError("购买失败,请刷新重试");
        }
    }

    /**
     * 中奖记录
     */
    public function prize_list(Request $request)
    {
        $user = $this->getUser();
        $order_id = $request->param("order_id/d");
        $whe = [];
        // $whe[] = ['user_id', '=', $user['id']];
        $whe[] = ['order_id', '=', $order_id];
        $whe[] = ['status', '=', '0'];
        $field = "id,order_id,user_id,goodslist_id,goodslist_title, goodslist_imgurl, goodslist_price,goodslist_money, goodslist_type,addtime,shang_id,goods_id,prize_type";
        $order = "id desc";
        $orderlist = OrderList::getAllList($whe, $field, $order);
        $goodslist_id = [];
        $goods_id = '';
        $prize_type = 1;
        foreach ($orderlist as $k => &$v) {
            $v['goodslist_imgurl'] = imageUrl($v['goodslist_imgurl']);
            // $v['prize_type'] = GoodsList::where(['id' => $v['goodslist_id']])->value('prize_type');
            $goodslist_id[] = $v['goodslist_id'];
            $shang = Shang::getInfo(['id' => $v['shang_id']], 'id,title');
            $v['shang_id'] = $shang['id'];
            $v['shang_title'] = $shang['title'];
            $goods_id = $v['goods_id'];
            if ($v['prize_type'] < 5) {
                $prize_type = $v['prize_type'];
            }
        }
        // $w = [];
        // $w[] = ['id', 'in', $goodslist_id];
        // $goods_list = GoodsList::getAllList($w, 'id, prize_type, goods_id');
        
        // $prize_types = array_column($goods_list->toArray(), 'prize_type');
        // if (in_array(2, $prize_types)) {
        //     $prize_type = 2;
        // }
        // if (in_array(3, $prize_types) && $prize_type == 1) {
        //     $prize_type = 3;
        // }
        // if (in_array(3, $prize_types) && $prize_type == 2) {
        //     $prize_type = 1;
        // }
        // if ($prize_type == 2) {
        //     $w = [];
        //     $w[] = ['id', 'in', $goodslist_id];
        //     $climb_num = GoodsList::where($w)->value('num');
        //     if ($climb_num == 10) {
        //         $prize_type = 4;
        //     }
        // }

        $w = [];
        $w[] = ['user_id', '=', $user['id']];
        $w[] = ['goods_id', '=', $goods_id];
        $num = ClimbUser::where($w)->order('id desc')->value('num') ?? 1;
        return $this->renderSuccess("请求成功", compact('orderlist', 'prize_type', 'num'));
    }

    /**
     * 榜单排行榜
     * @param Request $request
     */
    public function climb_ranks(Request $request)
    {
        $user = $this->getUser();
        $goods_id = $request->param("goods_id/d");
        $w = [];
        $w[] = ['goods_id', '=', $goods_id];
        $field = "id,user_id, count(number) as number";
        $order = "number desc, id desc";
        $ranks = ClimbRank::where($w)->field($field)->group('user_id')->order($order)->paginate(['list_rows' => 100, 'query' => request()->param()]);
        $data['list'] = $ranks->toArray()['data'];
        foreach ($data['list'] as $k => &$v) {
            $user_info = User::getInfo(['id' => $v['user_id']], 'id,nickname, headimg');
            $v['nickname'] = $user_info['nickname'];
            $v['headimg'] = imageUrl($user_info['headimg']);
            $v['rank_num'] = $k+1;
        }
        $page = $ranks->render();
        $data['count'] = $ranks->total();
        $data['last_page'] = $ranks->toArray()['last_page'];
        $data['page'] = $page;
        return $this->renderSuccess('请求成功', compact('data'));
    }
}